package com.ruoyi.flowable.listener.scrap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.equ.domain.EquScrap;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.flowable.listener.asset.AssetListener_2.getFileList;

/**
 * 报废-1监听：资产设备id、报废依据
 */
@Slf4j
@Component("scrapListener_1")
public class ScrapListener_1 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            //资产设备id
            Long assetId = Long.valueOf(execution.getVariable("assetId", String.class));

            /**
             * 报废依据
             */
            //报废依据:报废原因类型+补充说明+最近3次维修记录+设备现状
            //报废原因
            String scrapReason = execution.getVariable("scrapReason", String.class);
            //赋值
            HashMap<String, Object> scrapMap = new HashMap<>();
            scrapMap.put("报废原因", scrapReason);
            switch (scrapReason) {
                case "故障频发":
                    Integer repairTimes = Integer.valueOf(execution.getVariable("repairTimes", String.class));
                    BigDecimal repairCost = new BigDecimal(execution.getVariable("repairCost", String.class));
                    BigDecimal currentNetValue = new BigDecimal(execution.getVariable("currentNetValue", String.class));
                    scrapMap.put("近1年维修次数", repairTimes);
                    scrapMap.put("累计维修费用", repairCost);
                    scrapMap.put("当前净值", currentNetValue);
                    break;
                case "政策不符":
                    String lawName = execution.getVariable("lawName", String.class);
                    String lawClause = execution.getVariable("lawClause", String.class);
                    scrapMap.put("不符法规名称", lawName);
                    scrapMap.put("具体条款", lawClause);
                    break;
                case "其他":
                    String otherReason = execution.getVariable("otherReason", String.class);
                    scrapMap.put("补充说明", otherReason);
                    break;
            }

            //最近3次维修记录
            //赋值
            List<HashMap<String, Object>> repairList = new ArrayList<>();
            //1
            String repairTime1 = execution.getVariable("repairTime1", String.class);
            String repairContent1 = execution.getVariable("repairContent1", String.class);
            String repairCost1Str = execution.getVariable("repairCost1", String.class);
            HashMap<String, Object> repairContentMap1 = new HashMap<>();
            if (StringUtils.isNotEmpty(repairTime1) && StringUtils.isNotEmpty(repairContent1) && StringUtils.isNotEmpty(repairCost1Str)) {
                BigDecimal repairCost1 = new BigDecimal(repairCost1Str);
                repairContentMap1.put("维修时间", repairTime1);
                repairContentMap1.put("维修内容", repairContent1);
                repairContentMap1.put("维修费用", repairCost1);
                repairList.add(repairContentMap1);
            }
            //2
            String repairTime2 = execution.getVariable("repairTime2", String.class);
            String repairContent2 = execution.getVariable("repairContent2", String.class);
            String repairCost2Str = execution.getVariable("repairCost2", String.class);
            HashMap<String, Object> repairContentMap2 = new HashMap<>();
            if (StringUtils.isNotEmpty(repairTime2) && StringUtils.isNotEmpty(repairContent2) && StringUtils.isNotEmpty(repairCost2Str)) {
                BigDecimal repairCost2 = new BigDecimal(repairCost2Str);
                repairContentMap2.put("维修时间", repairTime2);
                repairContentMap2.put("维修内容", repairContent2);
                repairContentMap2.put("维修费用", repairCost2);
                repairList.add(repairContentMap2);
            }
            //3
            String repairTime3 = execution.getVariable("repairTime3", String.class);
            String repairContent3 = execution.getVariable("repairContent3", String.class);
            String repairCost3Str = execution.getVariable("repairCost3", String.class);
            HashMap<String, Object> repairContentMap3 = new HashMap<>();
            if (StringUtils.isNotEmpty(repairTime3) && StringUtils.isNotEmpty(repairContent3) && StringUtils.isNotEmpty(repairCost3Str)) {
                BigDecimal repairCost3 = new BigDecimal(repairCost3Str);
                repairContentMap3.put("维修时间", repairTime3);
                repairContentMap3.put("维修内容", repairContent3);
                repairContentMap3.put("维修费用", repairCost3);
                repairList.add(repairContentMap3);
            }

            //设备现状
            String appearanceDesc = execution.getVariable("appearanceDesc", String.class);
            String functionDesc = execution.getVariable("functionDesc", String.class);
            List<String> statusImages = getFileList(execution, "statusImages");
            //赋值
            HashMap<String, Object> equSituationMap = new HashMap<>();
            equSituationMap.put("外观描述", appearanceDesc);
            equSituationMap.put("功能描述", functionDesc);
            equSituationMap.put("设备现状照片", statusImages);

            HashMap<String, Object> scrapBasisMap = new HashMap<>();
            scrapBasisMap.put("报废原因", scrapMap);
            scrapBasisMap.put("最近3次维修记录", repairList);
            scrapBasisMap.put("设备现状", equSituationMap);
            String scrapBasis = objectMapper.writeValueAsString(scrapBasisMap);

            //附件链接
            List<String> contractFiles = getFileList(execution, "contractFiles");
            List<String> repairFiles = getFileList(execution, "repairFiles");
            List<String> testFiles = getFileList(execution, "testFiles");
            Map<String, Object> fileUrlsMap = new HashMap<>();
            fileUrlsMap.put("现状照片", statusImages);
            fileUrlsMap.put("购买合同/发票复印件", contractFiles);
            fileUrlsMap.put("历次维修记录单", repairFiles);
            fileUrlsMap.put("检测报告", testFiles);
            String fileUrls = objectMapper.writeValueAsString(fileUrlsMap);

            EquScrap equScrap = new EquScrap();
            equScrap.setAssetId(assetId);
            equScrap.setScrapBasis(scrapBasis);
            equScrap.setFileUrls(fileUrls);

            execution.setVariable("equScrap", equScrap);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
